package com.wuzuqing.android.eightdiagrams.utils

import com.wuzuqing.android.eightdiagrams.model.BaZiInfo
import com.wuzuqing.android.eightdiagrams.term.Bazhi
import com.wuzuqing.android.eightdiagrams.term.Solar

/**
 * @CopyRight: 广州仰望星空云科技有限公司
 * @Author : wuzuqing
 * @Email:wuzuqing@xinyu668.com
 * @CreateTime: 11.12.23 1:19 PM
 * @Description: 请写描述
 */
object BaZhiHelper {


    private fun genGanInfo(riGan: String, arr: Array<String>): BaZiInfo.GanInfo {
        val info = BaZiInfo.GanInfo()
        info.name = arr[0]
        info.wuXing = arr[1]
        info.shiShen = Bazhi.getShiShenStr(arr[0], riGan)
        return info
    }

    private fun genZhiInfo(riGan: String, arr: Array<String>): BaZiInfo.ZhiInfo {
        val info = BaZiInfo.ZhiInfo()
        info.name = arr[2]
        info.wuXing = arr[3]
        //日干五行,为了拿生克表
        val targetDiZhi = DataUtils.getDiZhiInfo(info.name.orEmpty())
        info.cangGan = targetDiZhi.cangGan.orEmpty()
        info.id = targetDiZhi.id
        info.ziZuo = DataUtils.getShiErShen(arr[0] + arr[2]).orEmpty()
        info.xinYun = DataUtils.getShiErShen(riGan + arr[2]).orEmpty()
        targetDiZhi.cangGan.toCharArray().forEach {
            info.shiShenList.add(Bazhi.getShiShenStr(it.toString(), riGan))
        }
        //0 =生我 1=比我 2=克我 3=我生 4=我克
        return info
    }
    private fun appendXiJi(info: BaZiInfo.BaseInfo, xiWuXing: String) {
        info.isLike = xiWuXing.contains(info.wuXing.orEmpty())
    }

    fun setDyInfo(info:BaZiInfo,gz:String){
        val riGan = info.riGanInfo?.name ?: return
        val arr = DataUtils.getGanZhi(gz)
        info.daYunGanInfo = genGanInfo(riGan, arr)
        info.daYunZhiInfo = genZhiInfo(riGan, arr)
        appendXiJi(info.daYunGanInfo!!, info.xiYongShen)
        appendXiJi(info.daYunZhiInfo!!, info.xiYongShen)
    }
    fun setLnInfo(info:BaZiInfo,gz:String){
        val riGan = info.riGanInfo?.name ?: return
        val arr = DataUtils.getGanZhi(gz)
        info.liuNianGanInfo = genGanInfo(riGan, arr)
        info.liuNianZhiInfo = genZhiInfo(riGan, arr)

        appendXiJi(info.liuNianGanInfo!!, info.xiYongShen)
        appendXiJi(info.liuNianZhiInfo!!, info.xiYongShen)
    }

    fun setLyInfo(info:BaZiInfo,gz:String){
        val riGan = info.riGanInfo?.name ?: return
        val arr = DataUtils.getGanZhi(gz)
        info.liuYueGanInfo = genGanInfo(riGan, arr)
        info.liuYueZhiInfo = genZhiInfo(riGan, arr)

        appendXiJi(info.liuYueGanInfo!!, info.xiYongShen)
        appendXiJi(info.liuYueZhiInfo!!, info.xiYongShen)
    }

    fun setLrInfo(info:BaZiInfo,gz:String){
        val riGan = info.riGanInfo?.name ?: return
        val arr = DataUtils.getGanZhi(gz)
        info.liuRiGanInfo = genGanInfo(riGan, arr)
        info.liuRiZhiInfo = genZhiInfo(riGan, arr)

        appendXiJi(info.liuRiGanInfo!!, info.xiYongShen)
        appendXiJi(info.liuRiZhiInfo!!, info.xiYongShen)
    }

    fun buildBaZhiInfo1(time: Long, solar: Solar, sexFlag: Boolean): BaZiInfo {
        val lunar = solar.lunar
        val baZiInfo = BaZiInfo()
        baZiInfo.sexType = if (sexFlag) 1 else 0
        baZiInfo.lunar = lunar
        val eightChar = lunar.getEightChar()
        baZiInfo.yun = lunar.getEightChar()?.getYun(baZiInfo.sexType)?.getDaYun(11)
        val nGZArr = DataUtils.getGanZhi(eightChar.yearGan+eightChar.yearZhi)
        val mGZArr = DataUtils.getGanZhi(eightChar.monthGan+eightChar.monthZhi)
        val dGZArr = DataUtils.getGanZhi(eightChar.dayGan+eightChar.dayZhi)
        val hGZArr = DataUtils.getGanZhi(eightChar.timeGan+eightChar.timeZhi)
        val riGan = dGZArr[0]
        baZiInfo.nianGanInfo = genGanInfo(riGan, nGZArr)
        baZiInfo.yueGanInfo = genGanInfo(riGan, mGZArr)
        baZiInfo.riGanInfo = genGanInfo(riGan, dGZArr)
        baZiInfo.shiGanInfo = genGanInfo(riGan, hGZArr)

        baZiInfo.nianZhiInfo = genZhiInfo(riGan, nGZArr)
        baZiInfo.yueZhiInfo = genZhiInfo(riGan, mGZArr)
        baZiInfo.riZhiInfo = genZhiInfo(riGan, dGZArr)
        baZiInfo.shiZhiInfo = genZhiInfo(riGan, hGZArr)

        baZiInfo.newDate = BaZiInfo.Date(solar.year, solar.month, solar.day, "")
        baZiInfo.time = time
        baZiInfo.lunarDate = BaZiInfo.Date(lunar.year, lunar.month, lunar.day, "")
        baZiInfo.coin = DataUtils.measureCoin(nGZArr, mGZArr, dGZArr, hGZArr)
        baZiInfo.coinType = DataUtils.measureCoin(baZiInfo)
        baZiInfo.xiYongShen = DataUtils.getXiYongShen(dGZArr[1], baZiInfo.coinType.coin)
        appendXiJi(baZiInfo.nianGanInfo!!, baZiInfo.xiYongShen)
        appendXiJi(baZiInfo.nianZhiInfo!!, baZiInfo.xiYongShen)
        appendXiJi(baZiInfo.yueGanInfo!!, baZiInfo.xiYongShen)
        appendXiJi(baZiInfo.yueZhiInfo!!, baZiInfo.xiYongShen)
        appendXiJi(baZiInfo.riZhiInfo!!, baZiInfo.xiYongShen)
        appendXiJi(baZiInfo.shiGanInfo!!, baZiInfo.xiYongShen)
        appendXiJi(baZiInfo.shiZhiInfo!!, baZiInfo.xiYongShen)
        baZiInfo.animalsYear = lunar.animalsYear()
        baZiInfo.taiYuan = eightChar?.taiYuan.orEmpty()
        baZiInfo.taiXi ="${DataUtils.getHeHuaZhi(baZiInfo.riGanInfo!!.name)}${DataUtils.getHeHuaZhi(baZiInfo.riZhiInfo!!
            .name)}"

        var tmp1 = (19 - baZiInfo.yueZhiInfo!!.id)%12
        if (tmp1==0){
            tmp1 = 12
        }
        LogUtils.d("tmp1:$tmp1 yueId:${baZiInfo.yueZhiInfo!!.id}")
        var tmp2 = (tmp1 - (baZiInfo.shiZhiInfo!!.id -1))%12
        if (tmp2<=0){
            tmp2 += 12
        }
        LogUtils.d("tmp2:$tmp2 riId:${baZiInfo.shiZhiInfo!!.id}")
        var tmp3 = ((baZiInfo.nianGanInfo!!.id%5)*2+1 + tmp2-3)%10
        if (tmp3<=0){
            tmp3 += 10
        }
        LogUtils.d("tmp3:$tmp3 rId:${baZiInfo.nianGanInfo!!.id}")
        baZiInfo.mingGong = eightChar?.mingGong.orEmpty()
        baZiInfo.dizhiShierlunModel = DataUtils.getDizhiShierlun(baZiInfo.yueZhiInfo!!.name)
        return baZiInfo
    }

//    fun buildBaZhiInfo(time: Long, birth: Birth, sexFlag: Boolean): BaZiInfo {
//        val baZiInfo = BaZiInfo()
//        baZiInfo.sexType = if (sexFlag) 1 else 0
//        val nGZArr = DataUtils.getGanZhi(birth.getcY())
//        val mGZArr = DataUtils.getGanZhi(birth.getcM())
//        val dGZArr = DataUtils.getGanZhi(birth.getcD())
//        val hGZArr = DataUtils.getGanZhi(birth.getcH())
//        val riGan = dGZArr[0]
//        baZiInfo.nianGanInfo = genGanInfo(riGan, nGZArr)
//        baZiInfo.yueGanInfo = genGanInfo(riGan, mGZArr)
//        baZiInfo.riGanInfo = genGanInfo(riGan, dGZArr)
//        baZiInfo.shiGanInfo = genGanInfo(riGan, hGZArr)
//
//        baZiInfo.nianZhiInfo = genZhiInfo(riGan, nGZArr)
//        baZiInfo.yueZhiInfo = genZhiInfo(riGan, mGZArr)
//        baZiInfo.riZhiInfo = genZhiInfo(riGan, dGZArr)
//        baZiInfo.shiZhiInfo = genZhiInfo(riGan, hGZArr)
//
//        baZiInfo.newDate = BaZiInfo.Date(birth.newYear, birth.newMonth, birth.newDay, "")
//        baZiInfo.time = time
//        baZiInfo.lunarDate = BaZiInfo.Date(birth.year, birth.month, birth.day, "")
//        baZiInfo.coin = DataUtils.measureCoin(nGZArr, mGZArr, dGZArr, hGZArr)
//        baZiInfo.xiYongShen = DataUtils.getXiYongShen(dGZArr[1], baZiInfo.coin)
//        appendXiJi(baZiInfo.nianGanInfo!!, baZiInfo.xiYongShen)
//        appendXiJi(baZiInfo.nianZhiInfo!!, baZiInfo.xiYongShen)
//        appendXiJi(baZiInfo.yueGanInfo!!, baZiInfo.xiYongShen)
//        appendXiJi(baZiInfo.yueZhiInfo!!, baZiInfo.xiYongShen)
//        appendXiJi(baZiInfo.riZhiInfo!!, baZiInfo.xiYongShen)
//        appendXiJi(baZiInfo.shiGanInfo!!, baZiInfo.xiYongShen)
//        appendXiJi(baZiInfo.shiZhiInfo!!, baZiInfo.xiYongShen)
//        baZiInfo.animalsYear = birth.animalsYear() + "年"
//        //"${Birth.Gan[(baZiInfo.yueGanInfo!!.id) % 10]}${Birth.Zhi[(baZiInfo.yueZhiInfo!!.id+2) % 12]}"
//        baZiInfo.taiYuan = "${Birth.Gan[(baZiInfo.yueGanInfo!!.id) % 10]}${Birth.Zhi[(baZiInfo.yueZhiInfo!!.id+2) % 12]}"
//        baZiInfo.taiXi ="${DataUtils.getHeHuaZhi(baZiInfo.riGanInfo!!.name)}${DataUtils.getHeHuaZhi(baZiInfo.riZhiInfo!!
//            .name)}"
//
//        //1 ->11=6      18%12
//        //2->12=5       17%12
//        //3 ->1 = 4     16%12
//        //4 ->2 = 3     15%12
//        //5 ->3= 2      14%12
//        //6 ->4 = 1     13%12
//        //7 ->5=12      12%12
//        //8 ->6=11
//        //9 ->7=10
//        //10 ->8=9
//        //11 ->9=8
//        //12 ->10=7     7%12
//        //
//        //6月 未时
//        //月支子时的支
//        var tmp1 = (19 - baZiInfo.yueZhiInfo!!.id)%12
//        if (tmp1==0){
//            tmp1 = 12
//        }
//        LogUtils.d("tmp1:$tmp1 yueId:${baZiInfo.yueZhiInfo!!.id}")
//        var tmp2 = (tmp1 - (baZiInfo.shiZhiInfo!!.id -1))%12
//        if (tmp2<=0){
//            tmp2 += 12
//        }
//        LogUtils.d("tmp2:$tmp2 riId:${baZiInfo.shiZhiInfo!!.id}")
//        var tmp3 = ((baZiInfo.nianGanInfo!!.id%5)*2+1 + tmp2-3)%10
//        tmp2--
//        if (tmp3<=0){
//            tmp3 += 10
//        }
//        LogUtils.d("tmp3:$tmp3 rId:${baZiInfo.nianGanInfo!!.id}")
//        baZiInfo.mingGong = "${Birth.Gan[tmp3-1]}${Birth.Zhi[tmp2]}"
//        baZiInfo.dizhiShierlunModel = DataUtils.getDizhiShierlun(baZiInfo.yueZhiInfo!!.name)
//        //  16   -> 3=3
//        //  27   -> 3=5
//        //  38   -> 3=7
//        //  49   -> 3=9
//        //  50   -> 3=1
//
//
//        //            val weekDay = "星期" + birth.getChinaWeekdayString(
////                calendar.time.toString().substring(0, 3)
////            ) //计算输出星期几
//        return baZiInfo
//    }
}